<?php

namespace App\Http\Controllers;

use App\Models\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Overtrue\Pinyin\Pinyin;
use Config;
use GuzzleHttp\Client;


class RegisterController extends Controller
{
    public function index()
    {
        $username = request()->fromuser;

        return view('member.login.register', compact('username'));
    }

    public function store(Request $request, AdminUser $user)
    {


        $this->validate($request, [
            'mt_login'     => 'required|unique:admin_users|min:6|max:15',
            'mt_login'     => 'integer',
            'username'     => 'required|unique:admin_users',
            'name'         => 'required',
            'phone'        => 'required|min:8|max:11',
            'email'        => [
                'required',
                'email',
//                function($b, $value, $fail) use ($request){
//
//                    $url = 'http://prod.bw.api.lwork.com/v1/api/user/ids?email='.$value;
//                    $http = new Client([
//                        'headers'    =>  [
//                            'x-api-tenantId'    => 'T002612',
//                            'x-api-token'       => 'Mk4p43N9gSBAebGr',
//                        ]
//                    ]);
//                    $result = json_decode($http->get($url)->getBody(true)->getContents(), true);
//                    if ($result['result']){
//                        $url2 = 'http://prod.bw.api.lwork.com/v1/api/user/ids?userIds='.$result['data']['userId'];
//                        $result2 = json_decode($http->get($url2)->getBody(true)->getContents(), true);
//                        if ($result2['result']){
//                            if ($result2['data']['name'] != $request->name || $result2['data']['email'] != $value){
//                                return $fail(trans('handlers.admin_user_zcsbjyzhy'));
//                            }
//                        }else{
//                            return $fail(trans('handlers.admin_user_wsycshzsx'));
//                        }
//                    }else{
//                        return $fail(trans('handlers.admin_user_wsycshzsx').'`');
//                    }
//                }
            ],
            'protocol'     => 'required',
            'tjr_user'     => [
                'required',
                function ($b, $v, $fail) {
                    if (!$res = AdminUser::where('username', $v)->first()) {
                        return $fail(trans('handlers.admin_user_tjrbczxx'));
                    }
                }
            ],
            'sfz_id'       => 'required|min:5|max:20',
            'address'      => 'required',
            'password'     => 'required',
            'pay_password' => 'required',
        ], [
            'username.required'     => trans('handlers.admin_user_dlzhbtxx'),
            'username.unique'       => trans('handlers.admin_user_zhbkyqcx'),
            'mt_login.integer'      => trans('handlers.admin_user_4zhzkwsz'),
            'mt_login.unique'       => trans('handlers.admin_user_4zhbkyqc'),
            'mt_login.required'     => trans('handlers.admin_user_4zhbtxxx'),
            'mt_login.min'          => trans('handlers.admin_user_4zhzs3wx'),
            'mt_login.max'          => trans('handlers.admin_user_4zhzd15w'),
            'name.required'         => trans('handlers.admin_user_xmbtxxxx'),
            'email.required'        => trans('handlers.admin_user_yjbtxxxx'),
            'email.email'           => trans('handlers.admin_user_yjdzbhfx'),
            'phone.required'        => trans('handlers.admin_user_sjhbtxxx'),
            'phone.min'             => trans('handlers.admin_user_sjhzs8wx'),
            'phone.max'             => trans('handlers.admin_user_sjhzd11w'),
            'tjr_user.required'     => trans('handlers.admin_user_tjrbtxxx'),
            'sfz_id.required'       => trans('handlers.admin_user_sfzhbtxx'),
            'sfz_id.min'            => trans('handlers.admin_user_sfzhzs5w'),
            'sfz_id.max'            => trans('handlers.admin_user_sfzhzd20'),
            'address.required'      => trans('handlers.admin_user_dzbtxxxx'),
            'password.required'     => trans('handlers.admin_user_dlmmbtxx'),
            'pay_password.required' => trans('handlers.admin_user_zfmmbtxx'),
            'protocol.required'     => trans('handlers.admin_user_qgxzcxyx'),
        ]);

        $res    = $user->where('username', $request->tjr_user)->first();
        $pinyin = new Pinyin(); // 默认

        $user->fill($request->all());
        $user->dry_password = encrypt(Config::get('app.dry_passwork').$user->password);
        $user->password     = bcrypt($user->password);
        $user->pay_password = bcrypt($user->pay_password);
        $result             = $pinyin->permalink($user->name, '');
        $user->en_name      = $result;
        $user->profit_rate  = 50;
        $user->created_at   = date('Y-m-d H:i:s');
        $user->updated_at   = date('Y-m-d H:i:s');
        unset($user->protocol);

        $user->tjr_id = $res->id;

        if ($user->save()) {
            $user->roles()->sync([2], false);

            return response()->json([
                'msg'    => trans('handlers.admin_user_zccgxxxx'),
                'status' => '1',
                'url'    => '/',
            ]);
        }
    }

}

